﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace GeoFly
{
    /// <summary>
    /// 栅格图层数据结构
    /// </summary>
    public class GridLayerPara
    {
        /// <summary>
        /// 参数读入
        /// </summary>
        /// <param name="FileName"></param>
        /// <returns></returns>
        public void ReadGridIOFile(string path)
        {
            //开始读取土壤及植被参数文件
            StreamReader sr = new StreamReader(path+"\\SysPara\\GridIO.txt");
            sr.ReadLine();
            sr.ReadLine();

            string[] strs= sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            DemFileName = strs[1];

            strs=sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            LulcFileName = strs[1];

            strs = sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            SoilFileName = strs[1];

            sr.ReadLine();

            strs = sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            int VegCount=Convert.ToInt16(strs[1]);
            for (int i = 0; i < VegCount; i++)
            {
                strs=sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                int key=Convert.ToInt16(strs[0]);
                VegTypes[key] = new VegInfo();
                VegTypes[key].Veg_Name= strs[1];
            }

            sr.ReadLine();

            strs=sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            int SoilCount = Convert.ToInt16(strs[1]);

            for (int i = 0; i < SoilCount; i++)
            {
                strs = sr.ReadLine().Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                int key = Convert.ToInt16(strs[0]);
                SoilTypes[key] = new SoilInfo();
                SoilTypes[key].Soil_Name = strs[1];
            }
            //读取土壤信息文件
            foreach (int key in SoilTypes.Keys)
            {
                string strFileName = path + "\\Soil\\" + SoilTypes[key].Soil_Name + ".sol";
                SoilTypes[key].ReadSoilFile(strFileName);
            }
            //读取植被信息文件
            foreach (int key in VegTypes.Keys)
            {
                string strFileName = path + "\\Vegetation\\" + VegTypes[key].Veg_Name + ".veg";
                VegTypes[key].ReadVegFile(strFileName);
            }
        }


        public void ReadBasinExtent(string FileName)
        {
            if (!File.Exists(FileName))
            {
                throw new Exception("错误");
            }

            int i = 2;
            List<string> saValue = MatrixFuncs.FileRead(FileName);
            int totrow = saValue.Count;
            string[] saOut = saValue[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);

            basinextent.xmin = Convert.ToDouble(saOut[1]);

            i += 1;
            saOut = saValue[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            basinextent.xmax = Convert.ToDouble(saOut[1]);

            i += 1;
            saOut = saValue[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            basinextent.ymin = Convert.ToDouble(saOut[1]);

            i += 1;
            saOut = saValue[i].Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
            basinextent.ymax = Convert.ToDouble(saOut[1]);

        }
        

        public void WriteGridIOFile(string FileName)//结果输出
        {
            StreamWriter sw = new StreamWriter(FileName);
            DateTime now=DateTime.Now;
            //写文件头
            sw.Write("GeoFly GridIO File:"+"\t"+now.Year.ToString("0000")+"-"+now.Month.ToString("00")+"-"+now.Day.ToString("00")+"\t");
            sw.WriteLine(now.Hour.ToString("00") + "\t" + now.Minute.ToString("00") + "\t" + now.Second.ToString("00"));
            sw.WriteLine();
            //写三个文件的文件名
            sw.WriteLine("DEM文件路径：\t"+HydroSimulate.g_GridLayerPara.DemFileName);
            sw.WriteLine("植被类型文件路径：\t"+HydroSimulate.g_GridLayerPara.LulcFileName);
            sw.WriteLine("土壤类型文件路径：\t"+HydroSimulate.g_GridLayerPara.SoilFileName);
            sw.WriteLine();
            sw.WriteLine("植被类型数量：\t"+HydroSimulate.g_GridLayerPara.VegTypes.Count);
            foreach (int key in HydroSimulate.g_GridLayerPara.VegTypes.Keys)
            {
                sw.WriteLine(key.ToString()+"\t"+HydroSimulate.g_GridLayerPara.VegTypes[key].Veg_Name);
            }
            sw.WriteLine();
            sw.WriteLine("土壤类型数量：\t" + HydroSimulate.g_GridLayerPara.SoilTypes.Count);
            foreach (int key in HydroSimulate.g_GridLayerPara.SoilTypes.Keys)
            {
                sw.WriteLine(key.ToString() + "\t" + HydroSimulate.g_GridLayerPara.SoilTypes[key].Soil_Name);
            }
            sw.Close();
        }
        public bool WriteBasinExtent(string FileName)
        {
            return false;
        }

        int GetSoilInfoID(int GridSoilValue)
        {
            return 0;
        }
        int GetVegInfoID(int GridVegValue)
        {
            return 0;
        }
        
        public string DemFileName;				//DEM图层文件名
        public string LulcFileName;				//LULC图层文件名
        public string SoilFileName;				//SOIL图层文件名	

        public SortedList<int, VegInfo> VegTypes = new SortedList<int, VegInfo>();
        public SortedList<int, SoilInfo> SoilTypes = new SortedList<int, SoilInfo>();
        
        /// <summary>
        /// DEM图层数据
        /// </summary>
        public GridLayer g_DemLayer;
        /// <summary>
        /// LULC植被\土地利用图层数据
        /// </summary>
        public GridLayer g_VegLayer;
        /// <summary>
        /// SOIL土壤图层数据
        /// </summary>
        public GridLayer g_SoilLayer;
        /// <summary>
        /// 流域边界
        /// </summary>
        public GridLayer g_BasinBoundary;
        public GridLayer g_StrahlerRivNet;
        public GridLayer g_RouteSurfQTime;
        public GridLayer g_RouteLatQTime;
        public GridLayer g_RouteBaseQTime;
        public GridLayer g_SubWaterShed;

        public int m_MaxStrahlerOrd;
        public int m_subNum;

        //public SoilInfoEx[,] pGridSoilInfo;
        //public VegInfo[,] pGridVegInfo;
        /// <summary>
        /// 土壤信息数组
        /// </summary>
        public SoilInfo[] pSoilInfo;
        /// <summary>
        /// 植被信息数组
        /// </summary>			
        public VegInfo[] pVegInfo;
        public MapExtent basinextent = new MapExtent();
        public int m_row;
        public int m_col;
    };
}
